﻿<div class="@GetCssClass()">
    @if (!string.IsNullOrEmpty(Label) && LabelPosition == LabelPosition.Left)
    {
        <label class="col-form-label col-form-label-sm me-2" @attributes="LabelAttributes()">@Label</label>
    }
    @ChildContent
    @if (!string.IsNullOrEmpty(Label) && LabelPosition == LabelPosition.Right)
    {
        <label class="col-form-label col-form-label-sm me-2" @attributes="LabelAttributes()">@Label</label>
    }
</div>
@code {
    [Parameter] public RenderFragment ChildContent { get; set; }
    [Parameter] public string Id { get; set; }
    [Parameter] public string CssClass { get; set; }
    [Parameter] public string Label { get; set; }
    [Parameter] public bool SmallSpacing { get; set; }
    [Parameter] public LabelPosition LabelPosition { get; set; } = LabelPosition.Left;

    IEnumerable<KeyValuePair<string, object>> LabelAttributes() {
        if(!string.IsNullOrEmpty(Id))
            yield return new KeyValuePair<string, object>("for", Id);
    }
    string GetCssClass() {
        List<string> cssClasses = new List<string>() { "option-component" };
        if(!string.IsNullOrWhiteSpace(CssClass))
            cssClasses.Add(CssClass);
        if(SmallSpacing)
            cssClasses.Add("small-spacing");
        return string.Join(' ', cssClasses);
    }
}
